<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>canvas</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        #canvas {

            display: block;
        }
    </style>
</head>

<body>
    <canvas id="canvas"></canvas>
    <script>
        alert('点击拖动或者触摸拖动，开始画画吧')
        var canvas = document.getElementById("canvas");
        canvas.width = document.documentElement.clientWidth//将canvas的宽度设置为屏幕的宽度
        canvas.height = document.documentElement.clientHeight//将canvas的高度设置为屏幕的高度
        let ctx = canvas.getContext('2d')
        ctx.fillStyle = "black";//设置画板颜色
        ctx.strokeStyle = 'none'
        ctx.lineWidth = 4
        ctx.lineCap = 'round'

        function drawLine(x1, y1, x2, y2) {
            ctx.beginPath()
            ctx.moveTo(x1, y1)//起点位置
            ctx.lineTo(x2, y2)//线的位置
            ctx.stroke()
        }
        let last
        //设备监听
        let isTouchDevice = 'ontouchstart' in document.documentElement;
        if (isTouchDevice) {

            canvas.ontouchstart = (e) => {
                last = [e.touches[0].clientX, e.touches[0].clientY]
            }
            canvas.ontouchmove = (e) => {
                let x = e.touches[0].clientX
                let y = e.touches[0].clientY
                drawLine(last[0], last[1], x, y)
                last = [x, y]
            }
        } else {
            console.log('这是PC端')
            let padding = false

            canvas.onmousedown = (e) => {
                last = [e.clientX, e.clientY]
                padding = true
            }
            canvas.onmouseup = (e) => {
                padding = false
            }
            canvas.onmousemove = (e) => {
                if (padding) {
                    drawLine(last[0], last[1], e.clientX, e.clientY)
                    last = [e.clientX, e.clientY]
                }
            }
        }




            // ctx.fillStyle = "rgba(0,0,200,0.5)"
            // ctx.fillRect = (30,30,55,50)


    </script>
</body>

</html>